$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var material = "";
    var pass = 1;

    //搜索框 获取焦点
    $("#goods_id").focus();
    $(".content").on("click", function () {
        $("#goods_id").val("").focus();
    });

    //点击 返回 箭头
    $(".head .goBack").on("click", function () {
        window.location.href = "divide_one.html";
    });

    //根据扫描内容 得到 原材料
    $(document).on("keydown", "#goods_id", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描回倒原料！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            $(".content .list-block ul .infinite-scroll-preloader").show();
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/divide/getDivideMaterial',
                data: {
                    divideId: localStorage.getItem("divideId"),
                    machine: localStorage.getItem("divideMachine"),
                    number: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    //加载提示符 隐藏
                    $(".content .list-block ul .infinite-scroll-preloader").hide();
                    if (res.success) {
                        var data = res.result;
                        //console.log(data);
                        $alert("扫描成功！");
                        material = data;
                        //填充 页面
                        $(".section span.number").html(data.batch);
                        $(".section span.name").html(data.inventoryName);
                        $(".section span.gggh").html(data.inventorySpecification);
                        $(".section span.mc").html(data.availableLength);
                        $(".section span.pcdh").html(data.workplanNumber);
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    //加载提示符 隐藏
                    $(".content .list-block ul .infinite-scroll-preloader").hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 取消 按钮
    $(".foot .quxiao").on("click", function () {
        window.location.href = "divide_one.html";
    });

    //点击 确定 按钮
    $(".foot .queding").on("click", function () {
        if (!material) {
            $alert("请扫描回倒原料！");
            setTimeout(function () {
                $("#goods_id").val("").focus();
            }, 1400);
        }
        else {
            if (pass == 0) {
                return
            }
            pass = 0;
            loading_show("上料中");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/divide/feedDivideMachine',
                data: {
                    // planId: material.plan._id,
                    divideId: localStorage.getItem("divideId"),
                    machine: localStorage.getItem("divideMachine"),
                    number: $(".section span.number").text()
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        loading_hide();
                        //上料 成功 跳转 页面
                        window.location.href = "divide_one.html";
                    } else {
                        loading_hide();
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

});
